package com.ziytek.web.citizen.configuration.db;

import com.ziytek.web.citizen.config.ConfigInit;
import jakarta.annotation.Resource;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;


/**
 * @author fengh
 */
public class DynamicMaxValidator implements ConstraintValidator<DynamicMax, Integer> {

    @Resource
    private ConfigInit configInit;
    private String configKey;

    @Override
    public void initialize(DynamicMax constraintAnnotation) {
        // 获取注解中配置的键
        this.configKey = constraintAnnotation.configKey();
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        // 允许为空（如需非空，可配合 @NotNull）
        if (value == null) {
            return true;
        }

        // 根据 configKey 动态获取最大值
        int maxValue = switch (configKey) {
            case "chargeCurrentLimitMax" -> configInit.getChargingCurrentLimitMax();
            case "chargeVoltageLimitMax" -> configInit.getChargingVoltageLimitMax();
            // 可扩展其他键
            default -> throw new IllegalArgumentException("未知的配置键: " + configKey);
        };

        return value <= maxValue;
    }
}